package com.atguigu.admin.controller;

import com.atguigu.admin.service.ProductService;
import com.atguigu.admin.utils.AliyunOSSUtils;
import com.atguigu.param.ProductSaveParam;
import com.atguigu.param.ProductSearchParam;
import com.atguigu.utils.R;
import com.google.j2objc.annotations.AutoreleasePool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.util.UUID;

/**
 * ClassName:ProductController
 * Package:com.atguigu.admin.controller
 * Description:
 *
 * @Author 应永豪
 * @Create 2024/4/23 15:22
 * @Version 1.0
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @Autowired
    private ProductService productService;
    @GetMapping("list")
    public R adminList(ProductSearchParam param){
        return productService.search(param);

    }

    @PostMapping("upload")
    public R adminUpload(@RequestParam("img")MultipartFile img) throws Exception {
        String originalFilename = img.getOriginalFilename();
        originalFilename= UUID.randomUUID().toString().replaceAll("-","")
                +originalFilename;

        String contentType = img.getContentType();
        byte[] bytes = img.getBytes();
        int hours =1000;


        String url = aliyunOSSUtils.uploadImage(originalFilename, bytes, contentType, hours);
        System.out.println("url+"+url);
        return R.ok("图片上传成功",url);


    }

    @PostMapping("save")
    public R adminSave(ProductSaveParam param){

        return productService.save(param);
    }

}
